package com.yugao.fintech.draper.admin.controller;

import com.yugao.fintech.draper.admin.service.TenantService;
import com.yugao.fintech.draper.admin.param.TenantInfoVO;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.security.annotation.SecurityInner;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.admin.param.TenantQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tenant")
@RequiredArgsConstructor
@Tag(name = "租户管理")
public class TenantController {
	private final TenantService tenantService;

	@Operation(summary = "查询租户列表分页")
	@GetMapping(value = "/page")
	public ResultVO<PageResult<TenantInfoVO>> pageQuery(@RequestBody TenantQuery tenantQuery) {
		return ResultVO.success(PageResult.success(tenantService.pageQuery(tenantQuery)));
	}

	@Operation(summary = "获取所有租户id集合")
	@GetMapping(value = "/getTenantIds")
	public List<Long> getTenantIds() {
		return tenantService.getTenantIds();
	}

	@Operation(summary = "校验租户信息是否合法")
	@SecurityInner
	@GetMapping(value = "/validTenant/{id}")
	public void validTenant(@PathVariable("id") Long id) {
		tenantService.validTenant(id);
	}

	@Operation(summary = "保存租户信息")
	@PostMapping(value = "/add")
	public ResultVO<Boolean> add(@RequestBody TenantInfoVO tenant) {
		return ResultVO.success(tenantService.addTenant(tenant));
	}

	@Operation(summary = "修改租户信息")
	@PostMapping(value = "/update")
	public ResultVO<Boolean> update(@RequestBody TenantInfoVO tenantInfoVO) {
		return ResultVO.success(tenantService.updateTenant(tenantInfoVO));
	}

	@Operation(summary = "删除租户信息")
	@DeleteMapping(value = "/delete")
	public ResultVO<Boolean> delete(Long id) {
		return ResultVO.success(tenantService.deleteByTenantId(id));
	}

	@Operation(summary = "根据id查询租户信息")
	@GetMapping(value = "/findById")
	public ResultVO<TenantInfoVO> findById(Long id) {
		return ResultVO.success(tenantService.findById(id));
	}

	@Operation(summary = "根据name查询租户Id")
	@SecurityInner
	@GetMapping(value = "/findIdByName")
	public ResultVO<Long> findTenantIdByName(String name) {
		return ResultVO.success(tenantService.findTenantIdByName(name));
	}

	@Operation(summary = "获取全部租户信息")
	@GetMapping(value = "/findAll")
	public ResultVO<List<TenantInfoVO>> findAll() {
		return ResultVO.success(tenantService.findAll());
	}

}